<!--
 * @Descripttion: (销售订单/V_PROD)
 * @Author: (admin)
 * @Date: (2025-06-19)
-->
<template>
  <div>
   <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
    <!-- <el-form-item label="在库状态" prop="ckstatus">
            <el-select v-model="queryParams.ckstatus"  placeholder="在库状态" clearable style="width: 140px" multiple>
              <el-option label="全部"  value="" />
              <el-option v-for="dict in options.statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
            </el-select>
          </el-form-item>-->
       <el-form-item> 
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      header-cell-class-name="el-table-header-cell"
      highlight-current-row
      @sort-change="sortChange"
      > 
     
      <el-table-column prop="sdate" label="日期 "  width="120" align="center" :show-overflow-tooltip="true"  />
       <el-table-column prop="code" label="工单号" align="center"  width="120" />
      <el-table-column prop="hzordeR1" label="计划令" align="center"  width="150" :show-overflow-tooltip="true"  />
      <el-table-column prop="maker" label="单位" align="center"  width="80" :show-overflow-tooltip="true"  />
      <el-table-column prop="ordcode" label="订单号" align="center"  width="100" :show-overflow-tooltip="true"  />
      <el-table-column prop="mprod" label="产品代码" align="center"  width="100" />
      <el-table-column prop="cname" label="品名" align="center"  width="120"/>
      <el-table-column prop="spec" label="规格" align="center" width="120" />
      <el-table-column prop="userflD1" label="图号" align="center"  />
        <el-table-column prop="qty" label="需求量" align="center"  />
              <el-table-column prop="gotqty" label="已领量" align="center" />
                      <el-table-column prop="comqty" label="完工量" align="center"  />
                              <el-table-column prop="uninqty" label="未入库" align="center"  />
                                <el-table-column prop="wipqty" label="在制数" align="center"  />
                                 <el-table-column prop="isEnd" label="状态" :show-overflow-tooltip="true"   />
                         <!-- <el-table-column label="操作" width="260">
        <template #default="scope">
          <el-button type="primary" size="small" icon="Lightning" title="详情" @click="handlePreview(scope.row)"></el-button>
            </template>
      </el-table-column> -->
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

 
  </div>
</template>

<script setup name="gbbomwork">
import { listSaleBOMWORK,  getSaleBOMWORK 
 } 
  from '@/api/buss/gbbomwork.js' 
  import { API_QueryAI } from '@/api/common.js' 
const { proxy } = getCurrentInstance()
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  PROD: undefined,
  ckstatus: ['在库','分配'], // 默认选中"在库"状态
  sort: 'MPROD',
  sortType: 'asc'
})
const  options= { 
    statusOptions: [
      { dictLabel: '在库', dictValue: '在库', listClass: 'success'  },
      { dictLabel: '出库', dictValue: '出库', listClass: 'danger' },
       { dictLabel: '分配', dictValue: '分配', listClass: 'danger'  }
    ]
  }

const aiDialog = ref()
const ids = ref([])
const loading = ref(false)
const route = useRoute()
const showSearch = ref(true)

watch(
  () => route.query.prod,
  (val) => {
    if (val) { 
      getListInit(val)
    }
  },
  {
    immediate: true
  }
)

 
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]
function   processSelectedStatus() {
  if (queryParams.ckstatus && queryParams.ckstatus.length > 0) {
    console.log('用户选择了:', queryParams.ckstatus.join(', '))
    // 这里可以添加您的处理逻辑
    return queryParams.ckstatus.join(',');
  } else {
    return ''
  }
}

function getListInit(prod){
  loading.value = true  
  queryParams.MPROD = prod;
  var parp={
  pageNum: 1,
  pageSize: 10,
  MPROD: prod, 
  sort: 'MPROD',
  sortType: 'asc'
  }  
  listSaleBOMWORK(parp).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}
function getList(){
  loading.value = true   
   
//  console.log(dateRange) 
  listSaleBOMWORK(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}
// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery(){
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}
 
/**
 * 查看
 * @param {*} row
 */
function handlePreview(row) {
  var data = {
    title: 'V_BOMWORK',
    Prompt: row.model,
  };
 API_QueryAI(data).then((res) => {
    const data = JSON.stringify(res.data.table1[0] || '无') ;
     const prom = JSON.stringify(res.data.table2[0].prompt||'无') ;
   //console.log(data)

    aiDialog.value?.openDialog()
  aiDialog.value?.clearMessages();
  // 准备提示词
  const prompt =  data +prom
// 请从以下几个方面进行分析：
// 1. 库存水平评估
// 2. 可能的补货建议
// 3. 潜在问题分析
// 4. 优化建议
  // 添加用户消息
  aiDialog.value?.addMessage({
    role: 'user',
    content:  prompt  
  })

  // 添加空的AI消息用于流式响应
  aiDialog.value?.addMessage({
    role: 'assistant',
    content: '',
    isStreaming: true
  })

  // 调用DeepSeek API
  aiDialog.value?.queryAI(prompt)
  })
  
}
 
  
</script>